package ferp.core.card;

import ferp.core.game.Game;

import java.util.Random;

/**
 * User: olegs
 * Date: 19/05/11 15:12
 */
public final class Deck
{
  // total size
  public static final int SIZE = 32;

  // get deck instance
  public static Deck instance() {return instance;}

  // get card object
  public Card card(int index) {return cards[index];}

  // shuffle cards
  public void shuffle()
  {
    // seed
    random.setSeed(System.currentTimeMillis());

    // Fisher–Yates - 7 times
    for (int p = 0; p < 7; ++p)
      for (int i = cards.length - 1; i > 0; --i)
        swap(i, random.nextInt(i + 1));
  }

  public int getRandomDealer() {return random.nextInt(Game.PLAYERS);}

  // deck instance
  private static final Deck instance = new Deck();

  // all card objects
  private final Card[] cards = new Card[SIZE];
  private final Random random = new Random();

  private Deck()
  {
    int i = 0;

    // fill the deck with card objects
    for (Card.Suit s : Card.Suit.rising)
      for (Card.Rank r : Card.Rank.values())
        cards[i++] = Card.card(s, r);
  }

  // swap 2 cards
  private void swap(int i, int j)
  {
    Card temp = cards[i];

    cards[i] = cards[j];
    cards[j] = temp;
  }
}
